package com.wei.tang.db;

import com.wei.tang.util.Tlog;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 
 * @author jamie
 *
 */
public class DBHelper extends SQLiteOpenHelper {
	/**
	 * 构造器
	 * 
	 * @param context
	 *            上下文
	 * @param name
	 *            数据库名称
	 * @param factory
	 *            游标工厂
	 * @param version
	 *            数据库版本
	 */
	public DBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	/**
	 * 构造DBHelper实例
	 * 
	 * @param content
	 *            上下文 数据库保存在当前应用包下的/database/目录下
	 */
	public DBHelper(Context context) {
		this(context, DBInfo.DB.DB_NAME, null, DBInfo.DB.VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {// 在数据库每一次被创建的时候调用
		db.execSQL(DBInfo.UserTable.CREATE_USER_TABLE);
		Tlog.i("weiketang", "用户表建好");
		db.execSQL(DBInfo.ArticleTable.CREATE_ARTICLE_TABLE);
		Tlog.i("weiketang", "文章表建好");
		db.execSQL(DBInfo.CourseTable.CREATE_COURSE_TABLE);
		Tlog.i("weiketang", "课程表");
		db.execSQL(DBInfo.CommentTable.CREATE_COMMENT_TABLE);
		Tlog.i("weiketang", "评论表建好");
		db.execSQL(DBInfo.ClassifyTable.CREATE_CLASSIFY_TABLE);
		Tlog.i("weiketang", "课程分类表建好");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(DBInfo.UserTable.DROP_USER_TABLE);
		onCreate(db);
		db.execSQL(DBInfo.ArticleTable.DROP_ARTICLE_TABLE);
		onCreate(db);
		db.execSQL(DBInfo.CourseTable.DROP_COURSE_TABLE);
		onCreate(db);
		db.execSQL(DBInfo.CommentTable.DROP_COMMENT_TABLE);
		onCreate(db);
		db.execSQL(DBInfo.ClassifyTable.DROP_CLASSIFY_TABLE);
		onCreate(db);
	}

}
